import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class DynamizerOld {

	private File simFile;
	private File caseFile;
	private File agentFile;
	private File paramsFile;
	private File initialSimFile;
	String initialSimFileName;
	private PrintWriter out;

	public DynamizerOld(String initialSimFileName) {
		this.initialSimFileName = initialSimFileName;
		// try {
		simFile = new File("./dynJavaSources/" + initialSimFileName
				+ "Sim.java");
		caseFile = new File("./dynJavaSources/" + initialSimFileName
				+ "Case.java");
		agentFile = new File("./dynJavaSources/" + initialSimFileName
				+ "Agent.java");
		paramsFile = new File("./dynJavaSources/" + initialSimFileName
				+ "Params.java");
		initialSimFile = new File("./dynJavaSources/" + initialSimFileName
				+ ".sim");
		// out = new PrintWriter(new FileWriter(simFile, false));
		// out.write("// C'est la classe!");
		// out.println();
		// out.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}

	public int compile(String sourcePath) {

		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

		return compiler.run(null, null, null, sourcePath);
	}

	public void loadData(String string, FileNameEnum fileName) {
		try {
			switch (fileName) {
			case SIM:
				out = new PrintWriter(new FileWriter(simFile, true));
				break;
			case AGENTS:
				out = new PrintWriter(new FileWriter(agentFile, true));
				break;
			case CASE:
				out = new PrintWriter(new FileWriter(caseFile, true));
				break;
			case PARAMS:
				out = new PrintWriter(new FileWriter(paramsFile, true));
				break;
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		// out.println();
		out.write(string);
		out.close();
	}

	public void loadClass(String data, FileNameEnum fileName) {
		try {
			loadData(data, fileName);
			File root = new File("./dynJavaSources/");
			URLClassLoader classLoader = URLClassLoader
					.newInstance(new URL[] { root.toURI().toURL() });
			String className = "";

			switch (fileName) {
			case SIM:
				className = initialSimFileName + "Sim";
				break;
			case AGENTS:
				className = initialSimFileName + "Agent";
				break;
			case CASE:
				className = initialSimFileName + "Case";
				break;
			case PARAMS:
				className = initialSimFileName + "Params";
				break;

			}

			 Class<? extends ImplementMe> myclass = (Class<? extends
			 ImplementMe>) Class
			 .forName(className, true, classLoader);
//			Class myclass = Class.forName(className, true, classLoader);

			Object object = myclass.newInstance();
//			object.doSomething();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public String fileToString() {
		String string = "";
		Scanner scanner;
		try {
			scanner = new Scanner(initialSimFile);
			while (scanner.hasNextLine()) {
				string = string + scanner.nextLine() + "\n";
			}
			scanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return string;
	}

	public ArrayList<String> elements(String string) {

		ArrayList<String> elements = new ArrayList<String>();

		StringTokenizer st = new StringTokenizer(string, "##");
		while (st.hasMoreTokens()) {
			elements.add(st.nextToken());
		}
		return elements;
	}

}
